layui.define(function(exports) {
    layui.use(['form', 'table','miniPage','element'], function () {
        let $ = layui.jquery, form = layui.form, table = layui.table, miniPage = layui.miniPage;

        table.render({
            elem: '#bookTableId',
            url: 'pageBorrowRecord',
            cellMinWidth: 80,
            // request:{
            //     username: $("#usernameSearchBorrowRecord").val()
            // },
            // toolbar: '#toolbarBookTable',
            // defaultToolbar: ['filter', 'exports'],
            defaultToolbar: [],
            cols: [[
                {
                    field: 'img',
                    width: '7%',
                    title: '封面',
                    align: "center",
                    fixed: 'left',
                    unresize: true,
                    sort: false,
                    // style:'height:48px;width:48px;line-height:48px!important;',
                    templet:function (d) {
                        return '<div name="imgBook" ><img src=\"' + d.imagePath + '\"alt="" width="50px" height="50px"></div>';
                    }
                },
                {field: 'id', width: '9%', title: 'id', hide: true, sort: true, align: "left"},
                {field: 'roleName', width: '7%', title: '角色', align: "left",align: "center"},
                {field: 'username', width: '7%', title: '用户名', align: "center"},
                {field: 'bookCode', width: '8%', title: '图书编号', align: "center"},
                {field: 'bookName', width: '12%', title: '图书名称',align: "center"},
                {field: 'publishHouse', title: '出版社', width: '12%',align: "center"},
                {field: 'qty', width: '5%', title: '数量', align: "center"},
                {field: 'imagePath', width: '5%', title: '封面图片地址', hide: true, sort: true,align: "center"},
                {field: 'borrowTime', width: '14%', title: '借书时间', sort: true,align: "center"},
                {field: 'returnTime', width: '14%', title: '还书时间', sort: true,align: "center"},
                {field: 'remainTime', width: '12%', title: '剩余时间', align: "center"},
                {field: 'ifBeyond', width: '8%', title: '是否超期', align: "center",
                    templet:function (d) {
                        if(d.ifBeyond){
                            return '<label style="color: red">是</label>';
                        }else{
                            return '<label style="color: green">否</label>';
                        }
                    }
                },
                {title: '操作', width: '13%', toolbar: '#bookTableOperateBar', align: "center", fixed: 'right'}
            ]],
            limits: [2, 10, 15, 20, 25, 50, 100],
            limit: 15,
            page: true,
            // skin: 'line' 加上没有竖线
            done: function(res,curr,count){
                $('div[name="imgBook"]').on('click',function(){
                    let bookImg = $(this).find("img");
                    //页面层
                    layer.open({
                        type: 1,
                        title: '封面',
                        skin: 'layui-layer-rim', //加上边框
                        area: ['500px', '500px'], //宽高
                        shadeClose: false, //开启遮罩关闭
                        end: function (index, layero) {
                            return false;
                        },
                        content: '<div style="text-align:center;width: 100%;height: 100%"><img style="height: 100%" src="' + bookImg.attr('src') + '" /></div>'
                    });
                });
            }
        });

        // 重新加载图书信息表格数据
        function reloadBookTableData(bookNo, bookName, username){
            table.reload('bookTableId', {
                page: {
                    curr: 1
                },
                where: {
                    bookNo: bookNo,
                    bookName: bookName,
                    username: username
                }
            }, 'data');
        }

        // 查看图书信息
        function viewBook(data){
            let content = miniPage.getHrefContent('page/book/borrowRecordView.html');
            let openWH = miniPage.getOpenWidthHeight();

            let index = layer.open({
                title: '查看详情',
                type: 1,
                shade: 0.2,
                maxmin:true,
                shadeClose: true,
                area: [openWH[0] + 'px', openWH[1] + 'px'],
                offset: [openWH[2] + 'px', openWH[3] + 'px'],
                content: content,
                success: function(layero, index){
                    layero.find("#bookImageView").attr("src", data.imagePath)
                    layero.find("#roleNameView").val(data.roleName)
                    layero.find("#usernameView").val(data.username)
                    layero.find("#bookCodeView").val(data.bookCode)
                    layero.find("#bookNameView").val(data.bookName)
                    layero.find("#bookQtyView").val(data.qty)
                    layero.find("#publishView").val(data.publishHouse)
                    layero.find("#borrowTimeView").val(data.borrowTime)
                },
            });
            $(window).on("resize", function () {
                layer.full(index);
            });
        }

        function returnBook(id){
            layer.confirm('确定还书吗？', {
                btn: ['确定', '取消']
            }, function (index) {
                let loadIndex = layer.load(0, {
                    shade: [0.3, '#8B8682']
                });
                $.ajax({
                    url: "returnBook",
                    data: {
                        id: id
                    },
                    type: "POST",
                    dataType: "json",
                    success: function (data) {
                        console.log(data);
                        if(data.code === -1){
                            layer.alert(data.msg, {
                                icon: 2,
                                skin: 'layer-ext-moon'
                            })
                        }else{
                            layer.alert(data.msg, {
                                icon: 1,
                                skin: 'layer-ext-moon'
                            })
                            // 关闭弹出层
                            layer.close(index);
                            parent.layui.table.reload('bookTableId');
                        }
                    },
                    error: function (data) {
                        layer.alert(data.responseText, {
                            icon: 2,
                            skin: 'layer-ext-moon'
                        })
                    }
                });
                // 关闭遮罩
                layer.close(loadIndex);
            }, function (index) {
                layer.close(index);
            });
        }

        // 清空按钮
        $("#cleanBtn").on("click", function(){
            $("input[name=bookNo]").val("");
            $("input[name=bookName]").val("");
            let dis = $("#usernameSearchBorrowRecord").attr("disabled");
            if(dis == undefined){
                $("#usernameSearchBorrowRecord").val("");
            }
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            let searchParam =data.field;
            reloadBookTableData(searchParam.bookNo, searchParam.bookName, searchParam.username)
            return false;
        });

        table.on('tool(currentTableFilter)', function (obj) {
            let data = obj.data;
            if(obj.event === 'view'){
                viewBook(data);
            }else if(obj.event === 'returnBook'){
                console.log(obj);
                returnBook(obj.data.id)
            }
        });

        $.ajax({
            url: "login/getLoginUserInfo",
            type: "POST",
            dataType: "json",
            success: function (data) {
                if(data.code === 0){
                    console.log("borrowRecord");
                    if("管理员" !== data.data.roleName){
                        $("#usernameSearchBorrowRecord").val(data.data.username);
                        $("#usernameSearchBorrowRecord").attr("disabled", "disabled");
                    }
                }
            },
            error: function (data) {
                layer.alert(data.responseText, {
                    icon: 2,
                    skin: 'layer-ext-moon'
                })
            }
        });
    });
})
